package it.unive.taw;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.PRIVATE_MEMBER;

/**
 * Servlet implementation class ServletIniziale
 */
public class ServletIniziale extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Initializes the servlet.
	 */
	public void init(ServletConfig config) throws ServletException {

		// try {
		// Class.forName ("com.mysql.jdbc.Driver").newInstance();
		// } catch (InstantiationException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// } catch (IllegalAccessException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// } catch (ClassNotFoundException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		// try {
		// Connection conn = DriverManager.getConnection ("", "", "");
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		super.init(config);

	}

	private void forward(HttpServletRequest request,
			HttpServletResponse response, String page) throws ServletException,
			IOException {
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(page);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String op = request.getParameter("op");
//		HttpSession session = request.getSession(true);

//		User u = (User) session.getAttribute("user");
//		PrintWriter out = response.getWriter();
		
		if (op == null || !"login".equals(op)) {
			forward(request, response, "/login1.jsp");
			return;
		}

		if ("login".equals(op)) {
			

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<html><head><title>Autenticazione</title></head><body><h2>Loginfiga</h2>" +
"<form action=\"<%=application.getContextPath()+\"/ServletIniziale\"%>" + "method=\"POST\">" +
"<input type=hidden name=\"op\" value=\"login\">" +
"<table>" +
"<tr><td>User:</td><td><input name=\"account\" type=\"text\"></td></tr>" +
"<tr><td>Password:</td><td><input name=\"password\" type=\"password\"></td></tr>" +
"<tr><td>&nbsp;</td><td><input type=\"submit\" value=\"OK\"></td></tr>" + "</table>" + "</form>" + 
"</body>" + "</html>" );
			out.close();
//			User u = new User( request.getParameter("account"),
//					request.getParameter("password"), 0, 0);
//			if ( u.verifica( "ciao", "figa" ) )
//				forward(request, response, "/home.jsp");
//			else {
////				session.setAttribute("user", u);
//				forward(request, response, "/login.jsp");
//			}
//			return;
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}
}
